package com.ihome.android.academy;

import android.media.AudioManager;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore.Video;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.widget.MediaController;
import android.widget.VideoView;

import com.ihome.android.EAActivity;
import com.ihome.android.annotation.EAInjectView;

//测试数据 url = rtsp://221.228.34.68:554/vod/23380900020010439627.mpg?userid=itvzhengh01&stbip=61.155.239.28&clienttype=1&mediaid=0000000030010009625363&ifcharge=1&time=20150803143724+08&life=172800&usersessionid=31135&vcdnid=vcdn001&boid=001&srcboid=001&columnid=391804&backupagent=221.228.34.68:554&ctype=1&playtype=0&Drm=0&EpgId=null&programid=23380900020010439627&contname=&fathercont=&bp=0&authid=&tscnt=0&tstm=0&tsflow=0&ifpricereqsnd=1&stbid=00100299007011500003C864C79D44D1&nodelevel=3&terminalflag=1&bitrate=0&usercharge=47D3B44B73B209A9CD3BE6DE83553308

public class VideoEduActivity extends EAActivity {

	@EAInjectView(id = R.id.edu_video)
	private VideoView edu_video;

	@EAInjectView(id = R.id.sv_sp)
	private VideoView sv_sp;

	private MediaPlayer player;
	int position;

	private MediaController mediaController;
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.edu_video);
		mediaController = new MediaController(this);
		edu_video.setMediaController(mediaController);



		player = new MediaPlayer();
		sv_sp.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
		sv_sp.getHolder().addCallback(new Callback() {
			public void surfaceDestroyed(SurfaceHolder arg0) {

			}

			public void surfaceCreated(SurfaceHolder arg0) {

				if(position>0){
					play();
					player.seekTo(position);
					position = 0;
				}
			}

			@Override
			public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {

			}
		});
	}

	private void play() {  
        try {  
        	player.reset();  
        	player  
            .setAudioStreamType(AudioManager.STREAM_MUSIC);  
            //设置需要播放的视频  
        	player.setDataSource("/mnt/sdcard/movie.3gp");  
            //把视频画面输出到SurfaceView  
        	player.setDisplay(sv_sp.getHolder());  
        	player.prepare();  
            //播放  
        	player.start();          
        } catch (Exception e) {  
            // TODO: handle exception  
        }  
    } 
	
	@Override
	protected void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		edu_video.setVideoURI(Uri.parse("http://www.zuiben.com/play/movie_412725.html"));
		edu_video.start();
	}

	@Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		mPositionWhenPaused = edu_video.getCurrentPosition();
		edu_video.stopPlayback();
	}

	private int mPositionWhenPaused;
	@Override
	protected void onResume() {
		// TODO Auto-generated method stub

		if(mPositionWhenPaused >= 0) {
			edu_video.seekTo(mPositionWhenPaused);
			mPositionWhenPaused = -1;
		}
		super.onResume();
	}

}
